package com.anelsoft.client;

import java.io.IOException;
import java.io.InputStream;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import com.anelsoft.api.fundamental.arquivo.ArquivoTexto;
import com.anelsoft.api.fundamental.banco.BD;
import com.anelsoft.api.fundamental.banco.ConexaoBD;
import com.anelsoft.api.fundamental.exception.BDException;

public final class Provider {

	public static ConexaoBD getConexaoBD() throws BDException, IOException {

		//      ResourceBundle bundle = ResourceBundle.getBundle("conexao_banco");

		ResourceBundle bundle = null;
		InputStream fis = Provider.class.getResourceAsStream("/conexao_banco.properties"); 
		//new FileInputStream(new File("conexao_banco.properties"));
		try {
			bundle = new PropertyResourceBundle(fis);
		} catch (Exception e) {
			throw new IOException(e);
		} 
		finally {
			if(fis != null) {
				fis.close();
			}
		} 

		return new BD(bundle).getConexaoBD();
	}

	public static String getTextoDeArquivo(String arquivo) throws IOException {
		ArquivoTexto at = null;
		try {
			at = new ArquivoTexto(arquivo);
			StringBuilder sb = new StringBuilder();
			while (at.leLinha()) {
				sb.append(at.getLinha());
				sb.append("\n");
			}
			return sb.toString();
		} finally {
			if(at != null) {
				at.fechar();
			}
		}
	}
}
